import { login, logout, getInfo} from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import { getRoleIdByuserId, getRoleIdByroleId ,getPermListByRoleId} from '@/api/auth'
import store from '@/store'
const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    menus: '',//新增
    userId: '',
    roleId: '',
    permList:'' //权限列表
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  // 新增
  SET_MENUS: (state, menus) => {
    state.menus = menus
  },
  // 新增用户Id
  SET_USERID: (state, userId) => {
    state.userId = userId
  },
  // 新增角色Id
  SET_ROLEID: (state, roleId) => {
    state.roleId = roleId
  },
  // 新增权限列表
  SET_PERMLIST: (state, permList) => {
    state.permList = permList
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    console.log("3==>开始执行vuex中的login方法 ")
    const { username, password ,vertify} = userInfo
    return new Promise((resolve, reject) => {
      console.log("4==>开始请求后端登录api ")
      login({ userName: username.trim(), password: password ,vertify:vertify}).then(response => {
        console.log("5==>后端登录api回调成功")
        // console.log(response);
        const { data } = response
        console.log(data);
        commit('SET_TOKEN', data)
        setToken(data)
        resolve()
      }).catch((error) => {
        console.log("后端登录api请求失败！")
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    console.log("12==>执行vuex中的getInfo方法，请求后端用户信息api")
    return new Promise((resolve, reject) => {
      getInfo({ token: state.token }).then(response => {
        const { data } = response
        console.log(data);
        console.log("14==>getInfo方法成功回调，保存name和头像到vuex中")
        if (!data) {
          return reject('Verification failed, please Login again.')
        }
        var vdata = data
        const { userName,userId } = data

       
        commit('SET_USERID', userId)
        commit('SET_NAME', userName)
        commit('SET_AVATAR', "https://www.wahaotu.com/uploads/allimg/202010/1604117432524136.jpg")
        resolve(vdata)
      }).catch(error => {
        reject(error)
      })
    })
  },
  getRoleIdByuserId({ commit}) {
    return new Promise((resolve, reject) => {
      const userId = store.getters.userId
      getRoleIdByuserId(userId).then(response => {
        const { data } = response
        commit('SET_ROLEID', data)
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  getRoleIdByroleId({ commit }) {
    return new Promise((resolve, reject) => {
      var roleId = store.getters.roleId
      getRoleIdByroleId(roleId).then(response => {
        const menus = response.data
        menus.push({
          path: '/404',
          component: '404',
          hidden: true,
        }, {
          path: '*',
          redirect: '/404',
          hidden: true,
        })
        commit("SET_MENUS", menus) // 触发vuex SET_MENUS 保存路由表到vuex
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  getPermListByRoleId({ commit }) {
    return new Promise((resolve, reject) => {
      var roleId = store.getters.roleId
      getPermListByRoleId(roleId).then(response => {
        const permList = response.data
        commit("SET_PERMLIST", permList) // 触发vuex SET_MENUS 保存路由表到vuex
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  // user logout
  logout({ commit, state }) {
    console.log("16==>调用用户退出api");
    return new Promise((resolve, reject) => {
      logout({token:state.token}).then(() => {
        console.log("17==>用户退出回调成功");
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

